package evote;

import java.util.ArrayList;

public class Permutation {
	
	static ArrayList<int[]> permutations(int[] a) {
	    ArrayList<int[]> ret = new ArrayList<int[]>();
	    permutation(a, 0, ret);
	    return ret;
	}

	public static void permutation(int[] arr, int pos, ArrayList<int[]> list){
	    if(arr.length - pos == 1)
	        list.add(arr.clone());
	    else
	        for(int i = pos; i < arr.length; i++){
	            swap(arr, pos, i);
	            permutation(arr, pos+1, list);
	            swap(arr, pos, i);
	        }
	}

	public static void swap(int[] arr, int pos1, int pos2){
	    int h = arr[pos1];
	    arr[pos1] = arr[pos2];
	    arr[pos2] = h;
	}
	//compute the list of all the permutations of size num
	public static  ArrayList<int[]> computeList (int num) {
		int [] array= new int [num];
 		for (int i=0; i<num ; i++) {
			array [i] = i;
		}
 		ArrayList<int[]> list = permutations(array);
 		return list;
	}
	// this func returns an array of size num*2 with the permutation of id1*40320+id2
	public static int[] run (int id1, int id2,int num,ArrayList<int[]> list) {
		int [] left= list.get(id1);
		int [] right= list.get(id2);
		int [] currentPer= new int [num*2]; 
 		for (int i=0;i<num;i++) {
 			currentPer[i] = left[i];
 		}
 		for(int i=0;i<num;i++) {
 			currentPer[i+num] = right[i] + num;
 		}
 		return currentPer;	
	}
	public static int[] run (int id, int num, ArrayList<int[]> list ) {
		int id1=id/40320;
		int id2=id%40320;
		return run(id1,id2,num,list);
	}
	
	public static void main(String args []) {
		ArrayList<int[]> list = computeList(8);
		int id = 40320*40320-1;
		int [] per = run(id,8,list);
		for (int i=0; i<16 ; i++) {
			System.out.print(per[i]+ " ");
		}
	}
	//todo, we need to know what is the size of permution
	public int GetSize(){
		return 0;
	}
	//todo I think all the funcs needs to be 
	
}
